package com.itheima.dubbo.api;

import cn.hutool.core.collection.CollUtil;
import com.itheima.model.mongo.UserLike;
import com.itheima.model.vo.CountsVo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.ArrayList;
import java.util.List;

@DubboService
public class UserLikeApiImpl implements UserLikeApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public Boolean saveOrUpdate(Long userId, Long likeUserId, boolean b) {
        try {
            //1.查询数据
            Query query = Query.query(Criteria.where("userId").is(userId).and("likeUserId").is(likeUserId));
            UserLike one = mongoTemplate.findOne(query, UserLike.class);

            //2.如果不存在，保存
            if(one==null){
                UserLike userLike = new UserLike();
                userLike.setCreated(System.currentTimeMillis());
                userLike.setUpdated(System.currentTimeMillis());
                userLike.setIsLike(b);
                userLike.setLikeUserId(likeUserId);
                userLike.setUserId(userId);
                mongoTemplate.save(userLike);
            }else {
                Update update = Update.update("isLike",b).set("created",System.currentTimeMillis());
                mongoTemplate.updateFirst(query,update,UserLike.class);
            }
            return true;
        } catch (Exception e) {
            return false;
        }
    }



    /**
     * 喜欢数统计
     * @param userId
     * @return
     */
    @Override
    public CountsVo counts(Long userId) {
        //喜欢的数量
        Query queryLove = Query.query(Criteria.where("userId").is(userId).and("isLike").is(true));
        int loveCount = (int) mongoTemplate.count(queryLove, UserLike.class);
        //粉丝数量
        Query queryFen = Query.query(Criteria.where("likeUserId").is(userId).and("isLike").is(true));
        int fanCount = (int) mongoTemplate.count(queryFen, UserLike.class);
        //所有喜欢的人
        Query query = Query.query(Criteria.where("userId").is(userId).and("isLike").is(true));
        List<UserLike> userLikes = mongoTemplate.find(queryLove, UserLike.class);
        List<Long> likeUserIds = CollUtil.getFieldValues(userLikes, "likeUserId", Long.class);
        //互相喜欢的数量
        int eachLoveCount = 0;
        for (Long likeUserId : likeUserIds) {
            Query query1 = Query.query(Criteria.where("userId").is(likeUserId).and("likeUserId").is(userId).and("isLike").is(true));
            boolean exists = mongoTemplate.exists(query1, UserLike.class);
            if (exists) {
                eachLoveCount++;
            }
        }
        CountsVo vo = new CountsVo(eachLoveCount,loveCount,fanCount);
        return vo;
    }
    //          查我的关注,需要返回关注的人的信息,
    @Override
    public List<UserLike> queryMyLike(Integer page, Integer pagesize, Long userId) {
        Pageable pageable = PageRequest.of(page-1,pagesize);
        Query query = Query.query(Criteria.where("userId").is(userId).and("isLike").is(true)).with(pageable);
        List<UserLike> userLikeList = mongoTemplate.find(query, UserLike.class);
        if (CollUtil.isEmpty(userLikeList)){
            return new ArrayList<>();
        }
        return userLikeList;
    }
    //查看对方是否关注了我
    @Override
    public Boolean queryIfLike(Long likeUserId, Long userId) {
        UserLike one = mongoTemplate.findOne(Query.query(Criteria.where("userId").is(likeUserId).and("likeUserId").is(userId).and("isLike").is(true)), UserLike.class);
        if (one!=null){
            return true;
        }
        return false;
    }

    @Override
    public List<UserLike> querylikeMe(Integer page, Integer pagesize, Long userId) {
        Pageable pageable = PageRequest.of(page-1,pagesize);
        Query query = Query.query(Criteria.where("likeUserId").is(userId).and("isLike").is(true)).with(pageable);
        List<UserLike> userLikeList = mongoTemplate.find(query, UserLike.class);
        if (CollUtil.isEmpty(userLikeList)){
            return new ArrayList<>();
        }
        return userLikeList;
    }
}
